﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.FrostLegion
{
    public class SnowmanHelicopter : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 8;
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Invasions.FrostLegion,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.NPCs.SnowmanHelicopter.Bestiary")
            ]);
        }
        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            var DropRules = Main.ItemDropsDB.GetRulesForNPCID(NPCID.SnowBalla, false);
            foreach (var DropRule in DropRules)
            {
                npcLoot.Add(DropRule);
            }
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (!NPC.AnyNPCs(144) || !Main.hardMode || spawnInfo.SpawnTileY >= Main.worldSurface)
            {
                return 0f;
            }
            return 0.04f;
        }
        public override void SetDefaults()
        {
            NPC.width = 50;
            NPC.height = 50;
            NPC.aiStyle = -1;
            NPC.damage = 60;
            NPC.defense = 28;
            NPC.lifeMax = 1200;
            NPC.HitSound = SoundID.NPCHit4;
            NPC.knockBackResist = 0.4f;
            NPC.DeathSound = SoundID.NPCDeath14;
            NPC.value = 1000f;
            NPC.noTileCollide = true;
            NPC.noGravity = true;
            NPC.npcSlots = 1.5f;
            AnimationType = 347;
            Music = MusicID.Boss3;
        }
        public override void AI()
        {
            NPC.TargetClosest();
            NPC.rotation = Math.Abs(NPC.velocity.X) * NPC.direction * 0.1f;
            NPC.spriteDirection = NPC.direction;
            float num995 = 7f;
            Vector2 vector132 = new(NPC.Center.X + NPC.direction * 20, NPC.Center.Y + 6f);
            float num996 = Main.player[NPC.target].position.X + Main.player[NPC.target].width * 0.5f - vector132.X;
            float num997 = Main.player[NPC.target].position.Y - vector132.Y;
            float num998 = (float)Math.Sqrt(num996 * num996 + num997 * num997);
            float num999 = num995 / num998;
            num996 *= num999;
            num997 *= num999;
            bool flag49 = Collision.CanHit(NPC.Center, 1, 1, Main.player[NPC.target].Center, 1, 1);
            if (num998 > 600f || !flag49)
            {
                int num1001 = 60;
                NPC.velocity.X = (NPC.velocity.X * (num1001 - 1) + num996) / num1001;
                NPC.velocity.Y = (NPC.velocity.Y * (num1001 - 1) + num997) / num1001;
                return;
            }
            NPC.velocity *= 0.98f;
            if (Math.Abs(NPC.velocity.X) < 1f && Math.Abs(NPC.velocity.Y) < 1f && Main.netMode != NetmodeID.MultiplayerClient)
            {
                NPC.localAI[0] += 1f;
                if (NPC.localAI[0] >= 15f)
                {
                    NPC.localAI[0] = 0f;
                    num996 = Main.player[NPC.target].position.X + Main.player[NPC.target].width * 0.5f - vector132.X;
                    num997 = Main.player[NPC.target].Center.Y - vector132.Y;
                    num996 += Main.rand.Next(-35, 36);
                    num997 += Main.rand.Next(-35, 36);
                    num996 *= 1f + Main.rand.Next(-20, 21) * 0.015f;
                    num997 *= 1f + Main.rand.Next(-20, 21) * 0.015f;
                    num998 = (float)Math.Sqrt(num996 * num996 + num997 * num997);
                    num995 = 10f;
                    num999 = num995 / num998;
                    num996 *= num999;
                    num997 *= num999;
                    num996 *= 1f + Main.rand.Next(-20, 21) * 0.0125f;
                    num997 *= 1f + Main.rand.Next(-20, 21) * 0.0125f;
                    int num1002 = Projectile.NewProjectile(NPC.GetSource_FromAI(), vector132.X, vector132.Y, num996, num997, 180, 32, 0f, Main.myPlayer);
                }
            }
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int num788 = 0; num788 < 7; num788++)
                {
                    int num789 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Smoke, 0f, 0f, 100, default, 1.5f);
                    Dust dust = Main.dust[num789];
                    dust.velocity *= 1.4f;
                }
                for (int num790 = 0; num790 < 4; num790++)
                {
                    int num791 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Torch, 0f, 0f, 100, default, 2.5f);
                    Main.dust[num791].noGravity = true;
                    Dust dust = Main.dust[num791];
                    dust.velocity *= 5f;
                    num791 = Dust.NewDust(new Vector2(NPC.position.X, NPC.position.Y), NPC.width, NPC.height, DustID.Torch, 0f, 0f, 100, default, 1.5f);
                    dust = Main.dust[num791];
                    dust.velocity *= 3f;
                }
                int num792 = Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X, NPC.position.Y), default, Main.rand.Next(61, 64));
                Gore gore2 = Main.gore[num792];
                gore2.velocity *= 0.4f;
                Main.gore[num792].velocity.X += 1f;
                Main.gore[num792].velocity.Y += 1f;
                num792 = Gore.NewGore(NPC.GetSource_Death(), new Vector2(NPC.position.X, NPC.position.Y), default, Main.rand.Next(61, 64));
                gore2 = Main.gore[num792];
                gore2.velocity *= 0.4f;
                Main.gore[num792].velocity.X -= 1f;
                Main.gore[num792].velocity.Y -= 1f;
            }
        }
    }
}
